<template>
  <CompOperate
      :field="field"
      :designer="designer"
      :design-state="designState"
      :parent-list="parentList"
      :parent-widget="parentWidget"
      :sub-form-row-id="subFormRowId"
      :sub-form-row-index="subFormRowIndex"
      :sub-form-col-index="subFormColIndex"
      :index-of-parent-list="indexOfParentList"
      :display-style="field['options'].displayStyle"
  >
    <div class="scene-waiting-art-widget">
      <div class="waiting-inside">
        <div
            class="waiting-item header-area"
            v-if="(optionsInfo.headShow)"
            :style="getStyle('head')"
        >
          <template v-for="(item) in (optionsInfo.colOptions)">
            <div class="item" v-if="getColShow(item.value)" :class="getClassName(item.value)" :style="setFlex(item)">
              {{ item.label }}
            </div>
          </template>
        </div>
        <div class="waiting-item list-area" :style="getStyle('list')">
          <template v-for="(item) in (waitingPatientInfos)">
            <div class="waiting-item-info">
              <template v-for="(items,index) in (optionsInfo.colOptions)">
                <template v-if="getColShow(items.value)">
                  <div class="item" :class="getClassName(items.value)" :style="setFlex(items)">
                    <template v-if="(items.value!=='treatingPatient')&&(items.value!=='waitPatientList')">
                      <!--人数-->
                      <template v-if="(items.value==='waitPatientCount')">
                        <span :data-value="`${item[items.value]}-${item.workerId}`"
                              :key="`${item[items.value]}-${item.workerId}`"
                              class="number-inside"
                              :class="{'item-background':(optionsInfo.itemShadow)}">
                          {{ item[items.value] || '\u00a0' }}
                        </span>
                      </template>
                      <!--除了人数-->
                      <template v-else>
                         <span :data-value="`${item[items.value]}-${item.workerId}`">
                           <ScrollX :speed="optionsInfo['listSpeed'] >10? 10:optionsInfo['listSpeed']"
                                    :key="`${item[items.value]}-${index}}`">
                                {{ getColContent(item, (items.value)) }}
                           </ScrollX>
                         </span>
                      </template>
                    </template>
                    <!--正在就诊-->
                    <template v-if="items.value==='treatingPatient'">
                      <span :class="{'item-background':(optionsInfo.itemShadow)}"
                            :data-value="`${item[items.value]}-${item.workerId}`"
                      >
                        <ScrollEditX :content="getCurrentPatient(item) || ''"
                                     :key="`${item[items.value]}-${item.workerId}`"/>
                      </span>
                    </template>
                    <!--候诊患者-->
                    <template v-if="items.value==='waitPatientList'">
                      <template v-if="(optionsInfo.openScroll)">
                        <vue-seamless-scroll
                            :ref="`scroll-${item.workerId}`"
                            class="scroll"
                            :key="`${item[items.value]}-${item.workerId}-${widgetKey}`"
                            @ScrollEnd="(e)=>handleScrollEnd(e, item)"
                            :class-option="classOptionTips()"
                            :data="item.waitPatientList"
                            :data-key="`${items.value}-${item.workerId}`"
                        >
                          <div class="inside-content" :class="{'patient_space':optionsInfo['patientSpace']}">
                              <span class="patient"
                                    v-for="(item,index) in (item.waitPatientList)"
                                    :key="`${(item.queuingId) || index}-${(item.patientIdNo) || index}`"
                              >

                            <template v-if="!!(item.patientName)">
                              {{ item.patientName }}
                            </template>
                            <span class="patient-orderNo" v-if="!!(item.orderNo)">
                              {{ item.orderNo }}
                            </span>
                          </span>
                          </div>
                        </vue-seamless-scroll>
                      </template>
                      <template v-else>
                        <div class="no-scroll">
                          <div class="inside-content" :class='getEllipsis'>
                          <span class="patient"
                                v-for="(item,index) in (item.waitPatientList)"
                                :key="`${(item.queuingId) || index}-${(item.patientIdNo) || index}-${index}`"
                          >
                            <template v-if="!!(item.patientName)">
                              {{ item.patientName }}
                            </template>
                            <span class="patient-orderNo" v-if="!!(item.orderNo)">
                              {{ item.orderNo }}
                            </span>
                          </span>
                          </div>
                        </div>
                      </template>
                    </template>
                  </div>
                </template>
              </template>
            </div>
          </template>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import {v4 as uuid} from 'uuid';
import {pxToVw} from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import vueSeamlessScroll from 'vue-seamless-scroll';
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import ScrollX from '@c/public/seamlessScroll/ScrollX.vue'
import ScrollEditX from '@c/public/seamlessScroll/ScrollEditX.vue'
import {buildParentApi} from "@/utils/designer/util";
import ScrollEditY from "@c/public/seamlessScroll/ScrollEditY.vue";

let buildParent = buildParentApi();

export default {
  name: "scene-waiting-art-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    devType: {
      type: Boolean,
      default: false,
    },

    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },

    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi()
    },
  },

  components: {
    ScrollEditY,
    ScrollX,
    ScrollEditX,
    CompOperate,
    vueSeamlessScroll,
  },

  data() {
    return {
      widgetKey: uuid(),
      optionsInfo: Object(),
    }
  },


  computed: {
    //生成唯一的字符串
    getOnlyId() {
      return function () {
        return uuid()
      }
    },
    // 温馨提示的滚动
    classOptionTips() {
      let {openScroll, waitingNumber, waitingVisitStep} = (this.optionsInfo);
      console.log(openScroll)
      console.log(waitingNumber)
      return function () {
        return {
          step: [1, 1.5, 2, 2.5, 3][waitingVisitStep - 1], // 数值越大速度滚动越快
          hoverStop: true,
          limitMoveNum: openScroll ? waitingNumber : 999999, // 开始无缝滚动的数据量 this.dataList.length
          direction: 2, // 0向下 1向上 2向左 3向右
          isSingleRemUnit: true,
        };
      }

    },

    // 栏目是否显示
    getColShow() {
      return function (val) {
        const self = this;
        let {defaultShowCol, mergeColInfo} = (self.optionsInfo) || Array();
        if (val === 'registerObjNames') {
          if (mergeColInfo && !!mergeColInfo[1] && !!mergeColInfo[1].length && (mergeColInfo[1].length) === 2) {
            return false
          } else {
            return defaultShowCol.includes(val);
          }
        } else {
          return defaultShowCol.includes(val);
        }

      };
    },

    // 拼接显示具体内容
    getColContent() {
      return function (val, key) {
        const self = this;
        let {mergeColInfo, registerObjNameKey, roomNameKey, deptNameKey} = (self.optionsInfo) || Object();
        switch (key) {
          case 'treatmentRoomName':
            if (mergeColInfo && !!mergeColInfo[1] && !!(mergeColInfo[1].length) && (mergeColInfo[1].length) === 2) {
              let mergeInfo = `${val[roomNameKey] || val[key] || '\u00a0'} ${val[registerObjNameKey] || '\u00a0'}`
              return self.$lodash.trim(mergeInfo);
            } else {
              return val[roomNameKey] || val[key] || '\u00a0'
            }
          case 'registerObjNames':
            return val[registerObjNameKey] || val[key];
          case 'deptName':
            return val[deptNameKey] || val[key];
          default:
            return val[key] || '\u00a0'
        }
      }
    },

    // 患者的ClassName
    getClassName() {
      return function (val) {
        switch (val) {
          case "treatingPatient":
            return "current-patient";
          case "waitPatientList":
            return "wait-patient";
          case "waitPatientCount":
            return "patient-count";
          case "registerObjNames":
            return "register-name";
          case 'deptName':
            return "deptName-name";
        }
      };
    },

    // 模板的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = (self.optionsInfo) || Array();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "color", (info.headerFontColor));
            Reflect.set(Obj, "height", pxToVw(info.headHeight));
            Reflect.set(Obj, "lineHeight", pxToVw(info.headHeight));
            Reflect.set(Obj, "backgroundColor", (info.headerBackgroundColor));
            return Obj;
          case "list":
            Reflect.set(Obj, "height", pxToVw(info.contentHeight));
            return Obj;
          case "listItem":
            Reflect.set(Obj, "lineHeight", pxToVw(info.lineHeight));
            Reflect.set(Obj, "height", pxToVw(info.lineHeight));
            return Obj;
        }
      };
    },

    // 设置列宽
    setFlex() {
      return function (item) {
        let Obj = Object();
        Reflect.set(Obj, "flex", (item.flex) || 1);
        return Obj;
      }
    },

    // 是否显示省略号
    getEllipsis() {
      const self = this;
      let {ellipsisState} = (self.optionsInfo);
      if (ellipsisState) {
        return 'ellipsis'
      }
    },

    // 当前就诊的患者|| 医生暂停接诊
    getCurrentPatient() {
      return function (val) {
        /**
         * 在线状态 0在线 1暂停 2退出
         */
        let type = parseInt(val.onlineStatus);
        switch (type) {
          case 0:
          case 2:
            if (!!(val.treatingPatient) && !!(val.treatingPatient.length)) {
              let currentPatient = Array();
              (val.treatingPatient).forEach((item) => {
                let temp = `${(item.patientName) || '\u00a0'}`;
                if (!!(item?.orderNo)) {
                  temp = `${temp}<span class="patient-orderNo">${(item.orderNo) || '\u00a0'}</span>`
                }
                let temps = this.$lodash.trim(temp);
                currentPatient.push(temps)
              })
              let info = this.$lodash.join(currentPatient, ' ')
              return (this.$lodash.trim(info));
            } else {
              return '\u00a0'
            }
          case 1:
            return '暂停接诊';
        }
      }
    },

    // 列表的队列的患者
    waitingPatientInfos() {
      let devType = (this.devType);
      let {showRowNums} = (this.field.options.waitingInfoConfig);
      let WaitingPatientList = this.$store.getters['getWaitingPatientInfo'];
      if (devType) {
        if (WaitingPatientList.apiData.length > showRowNums) {
          return WaitingPatientList.apiData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.apiData.length).fill({}));
          return [...WaitingPatientList.apiData, ...arr];
        }
      } else {
        if (WaitingPatientList.mockData.length > showRowNums) {
          return WaitingPatientList.mockData.slice(0, showRowNums);
        } else {
          let arr = (new Array(showRowNums - WaitingPatientList.mockData.length).fill({}));
          return [...WaitingPatientList.mockData, ...arr];
        }
      }
    },

  },

  mounted() {
    this.handleOnMounted();
  },

  methods: {
    // 等待候诊滚动结束
    handleScrollEnd(e, row) {
      const self = this;
      if (self.$refs[`scroll-${row.workerId}`]) {
        console.log('等待就诊滚动结束');
        self.$nextTick(() => {
          self.$refs[`scroll-${row.workerId}`][0] && self.$refs[`scroll-${row.workerId}`][0].reset()
        })
      }
    },

    // 接收父组件传递过来的数据
    async executeMethod(key, data) {
      switch (key) {
        case "patientListInfo":
          console.log(`患者列表-数据处理逻辑-${data}`);
          // this.uuid = uuid();
          let patientList = this.$lodash.cloneDeep(data);
          buildParent.executeParentMethod('writeLog', JSON.stringify({sign: '模板-患者信息', key: key, data: data}));
          await this.$store.dispatch('setWaitingPatientInfo', patientList)
          break
      }
    }
  },

  watch: {
    field: {
      handler: function (val) {
        this.widgetKey = uuid();
        let key = (val.options.config);
        this.optionsInfo = (val.options[key]);
      },
      deep: true,
      immediate: true
    }
  },

  destroyed() {
    this.unregisterFromRefList();
  },

};
</script>

<style lang="scss" scoped>
.scene-waiting-art-widget {
  background-color: transparent;
  color: #333;
  align-self: center;
  align-items: center;
  display: flex;
  flex-direction: column;
  height: 100%;
  line-height: 100%;
  box-sizing: border-box;
  margin: 20px 144px 10px 144px;
  overflow: hidden;
  backdrop-filter: blur(4px);

  .waiting-inside {
    border-radius: 20px !important;
    width: 100%;
    overflow: hidden;
    font-family: 'sharkFitHealth', serif !important;


    .waiting-item {
      width: 100%;
      display: flex;
      align-items: center;
      align-self: center;
      border-radius: 30px;
      overflow: hidden;

      flex-direction: unset;
      margin-bottom: 10px;
      flex-grow: 1;

      &:last-child {
        border-bottom: none !important;
      }

      .item {
        flex: 1;
        font-size: 38px;
        height: 100%;
        padding: 0 30px;
        text-align: center;
        font-weight: normal;
        justify-content: center;
        overflow: hidden;
        @include singleEllipsis(100%);

        //自定义水平滚动
        .scrollText {
          height: 100%;
          width: 100%;
          display: flex;
          align-self: center;
          justify-content: center;
          align-items: center;

          :deep(.st-scrolling) {
            padding-top: 22px;
          }

          :deep(.st-inner) {
            padding-top: 22px;
          }
        }

        .number-inside {
          padding-top: 22px;
        }
      }


      //等待的患者
      .wait-patient {
        flex: 5;
        display: flex;
        align-self: center;
        align-items: center;
        overflow: hidden;
        padding-right: 20px;

        :deep(.scroll) {
          width: 100%;
          overflow: hidden;

          > div {
            height: 100%;
            overflow: hidden;

            > div {
              height: 100%;
            }
          }


        }
      }

      //患者数量
      .patient-count {
        flex: 0.4;
        display: flex;
        align-items: center;
        align-self: center;
        justify-content: center;

        span {
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;
          height: 74px;
          width: 100%;
          max-width: 100%;
        }

        .item-inside {
          @include fontWeight();
        }

        .scroll-item-inside {
          padding-right: 0;
        }

      }

      //号别名称
      .register-name {
        flex: 1.5;
      }

      //当前患者
      .current-patient {
        flex: 1.5;

        .scrollText {
          height: 100%;
          width: 100%;
          display: flex;
          align-self: center;
          align-items: center;

          :deep(.st-inner) {
            padding-top: 2px;
          }

          :deep(.st-scrolling) {
            padding-top: 2px;
          }
        }
      }

      .wait-patient,
      .current-patient {
        :deep(.patient-orderNo) {
          margin-left: 4px;
          padding: 4px 8px;
          background: #14314B;
          font-size: 32px;
          min-width: 40px;
          color: #ffffff;
          border-radius: 8px;
          overflow: hidden;
          margin-right: 8px;
          height: 40px;
          line-height: 40px;
          align-self: center;
          align-items: center;
          text-align: center;
          display: inline-block;
          position: relative;
          top: 6px;
        }

        .scroll,
        .no-scroll {
          width: 100%;
          height: 80px;
          line-height: 80px;
          margin-top: 24px;
          overflow: hidden;

          > div {
            height: 100%;
            overflow: hidden;

            > div {
              height: 100%;
            }
          }
        }

        .inside-content {
          height: 100%;
          display: flex;
          align-self: flex-start;
          align-items: flex-start;
          overflow: hidden;
          text-align: left;

          .patient {
            height: auto;
            align-self: center;
            align-items: center;
            line-height: inherit;
            padding-right: 30px;
            font-size: 42px;
          }
        }


        .patient_space {
          margin-right: 120px;
        }

        .ellipsis {
          @include singleEllipsis(100%);
          display: block;
        }
      }
    }


    .header-area {
      width: 100%;
      display: flex;
      background-color: #0059b3;
      color: #fff;
      height: 110px;
    }

    .list-area {
      width: 100%;
      display: flex;
      height: auto;
      flex-direction: column;

      .waiting-item-info {
        display: flex;
        flex-grow: 1;
        width: 100%;
        height: 100%;
        background: white;
        margin-bottom: 10px;

        .item {
          color: #000000;
          border-right: 6px rgba(167, 217, 221, 1) solid;

          &:last-child {
            border-right: none;
          }

          .item-background {
            border-radius: 14px;
            overflow: hidden;
            background: rgba(0, 89, 179, 0.2);
          }
        }

        //子元素背景色
        .item-background {
          border-radius: 14px;
          overflow: hidden;
          padding: 0 24px;
          background: rgba(0, 89, 179, 0.2);
        }
      }
    }
  }
}
</style>